package com.hundsun.cprs.yyt.web.action.system;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.alibaba.fastjson.JSONObject;
import com.hundsun.cprs.yyt.common.exception.ServiceCodeException;
import com.hundsun.cprs.yyt.service.system.domain.BussinessParam;
import com.hundsun.cprs.yyt.service.system.domain.query.BussinessParamQuery;
import com.hundsun.cprs.yyt.service.system.service.BussinessParamService;
import com.hundsun.cprs.yyt.web.action.BaseAction;
import com.hundsun.network.eclp.ex.common.PermissionEnum;
import com.hundsun.network.eclp.ex.security.AdminAccess;
@Controller
@RequestMapping("system/")
public class BussinessAction extends BaseAction{
	
	@Autowired
	private BussinessParamService bussinessParamService;
	
	@AdminAccess(PermissionEnum.SYSTEM_BUSSINISE_PARAMTER)
	@RequestMapping("business/list.htm")
	public String queryList(ModelMap model,@ModelAttribute("query")BussinessParamQuery query){
		/*BussinessParam bussinessParam = bussinessParamService.getBussinessParamByKey(EnumBussinessParam.Goods_OUTTIME.getCode());
		model.addAttribute("bussinessParam", bussinessParam);*/
		List<BussinessParam> bussinessParamList=bussinessParamService.queryBussinessParamList(query);
		model.addAttribute("bussinessParamList", bussinessParamList);
		return "system/bussinessList";
	}
	
	@RequestMapping("bussiness/parameter/update.htm")
	public void updateParam(HttpServletResponse response,@RequestParam("id")String id,@RequestParam("value")String value,@RequestParam("key")String key)throws IOException{
		JSONObject json = new JSONObject();
		BussinessParam bussinessParam = new BussinessParam();
		bussinessParam.setId(Long.parseLong(id));
		bussinessParam.setBussinessValue(value);
		bussinessParam.setBussinessKey(key);
		try{
			bussinessParamService.updateBussinessParamValue(bussinessParam);
		}catch(ServiceCodeException e){
			log.error(e);
			json.put("result", "error");
		}		
		json.put("result", "success");			
		try {
			response.getWriter().print(json);
		} catch (Exception e) {
			log.error(e, e);
		}
	}
}
